package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.CaseAlarmPanel;
import com.battle.pojo.enums.CaseDispatchTypeEnum;
import com.battle.pojo.param.caseAlarmPanel.*;
import com.battle.pojo.vo.caseAlarmPanel.*;
import com.battle.pojo.dto.caseAlarmPanel.*;
import com.battle.service.repository.in.CaseAlarmPanelRepository;
import com.battle.service.service.in.CaseAlarmPanelService;
import com.battle.service.service.in.CaseDispatchService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：接处警-战例信息服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class CaseAlarmPanelServiceImpl implements CaseAlarmPanelService {

    private final CaseAlarmPanelRepository caseAlarmPanelRepository;
    private final CaseDispatchService caseDispatchService;
    public CaseAlarmPanelServiceImpl(CaseAlarmPanelRepository caseAlarmPanelRepository, CaseDispatchService caseDispatchService) {
        this.caseAlarmPanelRepository = caseAlarmPanelRepository;
        this.caseDispatchService = caseDispatchService;
    }


    /**
     * 创建接处警-战例信息
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCaseAlarmPanel(CreateCaseAlarmPanelParam param) {
        CaseAlarmPanel caseAlarmPanel = new CaseAlarmPanel();
        BeanUtils.copyProperties(param, caseAlarmPanel);
        Long id=param.getCaseId();
        caseAlarmPanel.setId(id);
        caseAlarmPanelRepository.create(caseAlarmPanel);

        //接处警-指挥中心力量调派情况
        caseDispatchService.createCaseDispatch(true, param.getPatrolStationCommandList(), CaseDispatchTypeEnum.patrolStationCommand, id, caseAlarmPanel.getSirensId());
        //接处警-消火站调度情况
        caseDispatchService.createCaseDispatch(true, param.getSmotheringStationList(), CaseDispatchTypeEnum.smotheringStation, id, caseAlarmPanel.getSirensId());
        //接处警-微站调度情况
        caseDispatchService.createCaseDispatch(true, param.getPatrolStationMobilizationForceList(), CaseDispatchTypeEnum.patrolStationMobilizationForce,id, caseAlarmPanel.getSirensId());
    }

    /**
     * 修改接处警-战例信息
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCaseAlarmPanel(EditCaseAlarmPanelParam param) {
        Long id = param.getCaseId();
        CaseAlarmPanel caseAlarmPanel = caseAlarmPanelRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, caseAlarmPanel);
        caseAlarmPanelRepository.edit(caseAlarmPanel);

        //接处警-指挥中心力量调派情况
        caseDispatchService.createCaseDispatch(true, param.getPatrolStationCommandList(), CaseDispatchTypeEnum.patrolStationCommand, id, caseAlarmPanel.getSirensId());
        //接处警-消火站调度情况
        caseDispatchService.createCaseDispatch(true, param.getSmotheringStationList(), CaseDispatchTypeEnum.smotheringStation, id, caseAlarmPanel.getSirensId());
        //接处警-微站调度情况
        caseDispatchService.createCaseDispatch(true, param.getPatrolStationMobilizationForceList(), CaseDispatchTypeEnum.patrolStationMobilizationForce,id, caseAlarmPanel.getSirensId());
    }

    /**
     * 移除接处警-战例信息
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCaseAlarmPanel(Long id) {
        caseAlarmPanelRepository.removeById(id);

        //接处警-指挥中心力量调派情况
        caseDispatchService.createCaseDispatch(true, null, CaseDispatchTypeEnum.patrolStationCommand, id, null);
        //接处警-消火站调度情况
        caseDispatchService.createCaseDispatch(true, null, CaseDispatchTypeEnum.smotheringStation, id, null);
        //接处警-微站调度情况
        caseDispatchService.createCaseDispatch(true, null, CaseDispatchTypeEnum.patrolStationMobilizationForce,id,null);
    }

    /**
     * 启动/禁用接处警-战例信息
     *
     * @param id
     */
    @Override
    public void enabledCaseAlarmPanel(Long id) {
        CaseAlarmPanel caseAlarmPanel = caseAlarmPanelRepository.getByIdCheck(id);
        caseAlarmPanel.setIsDisable(!caseAlarmPanel.getIsDisable());
        caseAlarmPanelRepository.edit(caseAlarmPanel);
    }

    /**
     * 获取接处警-战例信息详情
     *
     * @param id
     * @return
     */
    @Override
    public GetCaseAlarmPanelVo getCaseAlarmPanel(Long id) {
        CaseAlarmPanel caseAlarmPanel = caseAlarmPanelRepository.getById(id);
        GetCaseAlarmPanelVo vo = new GetCaseAlarmPanelVo();
        if (caseAlarmPanel == null) {
            return vo;
        }
        BeanUtils.copyProperties(caseAlarmPanel, vo);

        //接处警-指挥中心力量调派情况
        vo.setPatrolStationCommandList(caseDispatchService.getCaseDispatchDownList(id, CaseDispatchTypeEnum.patrolStationCommand));
        //接处警-消火站调度情况
        vo.setSmotheringStationList(caseDispatchService.getCaseDispatchDownList(id, CaseDispatchTypeEnum.smotheringStation));
        //接处警-微站调度情况
        vo.setPatrolStationMobilizationForceList(caseDispatchService.getCaseDispatchDownList(id, CaseDispatchTypeEnum.patrolStationMobilizationForce));

        return vo;
    }

    /**
     * 获取接处警-战例信息 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetCaseAlarmPanelListVo> getCaseAlarmPanelList(GetCaseAlarmPanelListParam param) {

        SelectCaseAlarmPanelDto dto = new SelectCaseAlarmPanelDto();
        BeanUtils.copyProperties(param, dto);
        Page<CaseAlarmPanel> page = caseAlarmPanelRepository.selectCaseAlarmPanel(dto);
        List<CaseAlarmPanel> caseAlarmPanelList = page.getRecords();
        List<GetCaseAlarmPanelListVo> list = new ArrayList<>();
        for (CaseAlarmPanel caseAlarmPanel : caseAlarmPanelList) {
            GetCaseAlarmPanelListVo vo = new GetCaseAlarmPanelListVo();
            BeanUtils.copyProperties(caseAlarmPanel, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取接处警-战例信息下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetCaseAlarmPanelDownListVo> getCaseAlarmPanelDownList(GetCaseAlarmPanelDownListParam param) {
        CaseAlarmPanel po = new CaseAlarmPanel();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<CaseAlarmPanel> caseAlarmPanelList = caseAlarmPanelRepository.getListByEntity(po);

        List<GetCaseAlarmPanelDownListVo> list = new ArrayList<>();
        for (CaseAlarmPanel caseAlarmPanel : caseAlarmPanelList) {
            GetCaseAlarmPanelDownListVo vo = new GetCaseAlarmPanelDownListVo();
            BeanUtils.copyProperties(caseAlarmPanel, vo);
            list.add(vo);
        }
        return list;
    }


}



